package cn.tedu.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ClassDemo2 {
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        //字节码对象
        Class<String> clz = String.class;

        //调用newInstance方法返回实例对象
        //调用newInstance无参方法时就是在执行无参构造
        //String s = clz.newInstance();

        //
        // String s=new String("abc");
        //返回构造方法（需要传入参数的字节码对象）
        Constructor<String> c = clz.getConstructor(String.class);
        //给有参构造传值执行有参构造返回实例对象
        String s = c.newInstance("abc");




        //通过有参构造返回Integer类实例对象
        //字节码对象
        Class<Integer> clz1 = Integer.class;

        //查找有参构造
        Constructor<Integer> c1 = clz1.getConstructor(int.class);

        //调用newInstance有参方法进行赋值执行有参构造返回实例对象
        Integer in = c1.newInstance(123);

        System.out.println(in);
    }
}
